

#import "BaseViewController.h"
#import "IQKeyboardManager.h"
#import "Tool.h"
@interface BaseViewController ()

@property (nonatomic, strong, readonly) NSArray * textViewArray;

@property (nonatomic, strong) UIView * keyBoard_v;

@property (nonatomic, assign) CGRect  tabbarRec;

@end

@implementation BaseViewController

NSString *GetClassStr(NSArray <NSArray <NSString *> *> * arr , NSIndexPath *indexPath){
    NSArray *arrA = nil;
    if (indexPath.section >= arr.count) {
        arrA = arr.lastObject;
        
    }else{
        arrA = arr[indexPath.section];
    }
    
    
    if (indexPath.row >= arrA.count)
        return arrA[0];
    return arrA[indexPath.row];
}

NSArray <NSString *> * CellArr(NSArray <NSArray <NSString *> *> * arr){
    NSMutableArray *cellArr = [NSMutableArray array];
    [arr enumerateObjectsUsingBlock:^(NSArray<NSString *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (![cellArr containsObject:obj]) {
                [cellArr addObject:obj];
            }
        }];
    }];
    return cellArr;
}


-(void)viewDidDisappear:(BOOL)animated{
 
    [self.view endEditing:YES];
       [super viewDidDisappear:animated];
}

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    if (self.hidNav) {
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:CC_CURRENTVC object:self];
   
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.hidNav) {
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }

}

-(void)dealloc{
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backUpRootView)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self noFrameForIos9];
    [self detailUI];
    [self tableviewResCel];
}

- (void)detailUI{}

- (void)needLayoutForIOS_9{
    if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_9_x_Max ) {
        for (NSLayoutConstraint *constraint in self.view.constraints) {
            if (constraint.firstItem == self.view || constraint.secondItem == self.view) {
                if (constraint.firstAttribute == NSLayoutAttributeTop || constraint.secondAttribute == NSLayoutAttributeTop) {
                    if (constraint.constant == 0) {
                        constraint.constant = 64;
                    }
                }
            }
        }
    }
}


- (void)noFrameForIos9{
    [self needLayoutForIOS_9];
}
- (void)getData:(NSInteger)page{}
- (void)tableviewResCel{}
- (void)backUpRootView
{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addRightBarItemWithData:(id)data withSEL:(SEL)sel{
    if ([data isKindOfClass:[UIImage class]]) {
         self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[data imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:sel];
      
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:data style:UIBarButtonItemStylePlain target:self action:sel];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    }

}
- (void)addLeftBarItemWithData:(id)data withSEL:(SEL)sel{
    if ([data isKindOfClass:[UIImage class]]) {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[data imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:sel];
    }else{
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:data style:UIBarButtonItemStylePlain target:self action:sel];
        self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    }
}

- (void)setCustomNavWithTitleColor:(UIColor *)titleColor withBackColor:(UIColor *)backColor{
 self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:TrueFont(20)]};
    self.navigationItem.leftBarButtonItem.tintColor = titleColor;
//    self.navigationItem.rightBarButtonItem.tintColor = titleColor;
    self.navigationController.navigationBar .tintColor = backColor;
    self.navigationController.navigationBar .barTintColor = backColor;
    
}

- (void)setNavColor{
    [self setCustomNavWithTitleColor:[UIColor whiteColor] withBackColor:[UIColor whiteColor]];
}




- (void)IQKeyBoardIsCanNotWork:(NSArray *)textViewArray{
    [IQKeyboardManager sharedManager].enable = NO;
    _textViewArray = [NSMutableArray arrayWithArray:textViewArray];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardShow:(NSNotification *)notification{
    
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notification.userInfo];
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [self.view addSubview:self.keyBoard_v];
    
    void (^animation)(void) = ^void(void) {
        for (UITextView *textView in _textViewArray) {
            if (textView.isFirstResponder) {                
                CGFloat height = YYScreenHeight() - textView.bottomForWindow - keyBoardHeight;
                //小于零说明有重叠   就是覆盖了  需要把覆盖的高度移出来
                if (height < 0) {
                    self.view.transform = CGAffineTransformMakeTranslation(0, height);
                }
                break;
            }
        }
    };
    
    [UIView animateWithDuration:animationTime animations:animation];
}


- (void)keyboardHide :(NSNotification *)notification{
    
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:notification.userInfo];
    
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    void (^animation)(void) = ^void(void) {
        self.view.transform = CGAffineTransformIdentity;
        [self removeKeyBoard_v];
    };
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    }
    
}

- (void)tapBack:(UITapGestureRecognizer *)tap{
    [self.view endEditing:YES];
}

-(UIView *)keyBoard_v{
    if (!_keyBoard_v) {
        _keyBoard_v = [[UIView alloc] initWithFrame:CGRectMake(0,64, YYScreenWidth(), YYScreenHeight() - 64)];
        _keyBoard_v.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBack:)];
        [_keyBoard_v addGestureRecognizer:tap];
    }
    return _keyBoard_v;
}



- (void)removeKeyBoard_v{
    [self.keyBoard_v removeFromSuperview];
    _keyBoard_v = nil;
}

- (void)pushVC:(BaseViewController *)vc{
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)setButtonState:(UIButton *)button{
    [self.view layoutIfNeeded];
    button.backgroundColor = UIColorHex(@"D70D02");
    button.layer.cornerRadius = button.height / 2;
    button.layer.shadowColor = UIColorHex(@"D70D02").CGColor;
    button.layer.shadowOpacity = 0.5;
    button.layer.shadowRadius = 5.f;
    button.layer.shadowOffset = CGSizeMake(0,3);
}




@end
